/**
 * May 2, 2014 2:44:13 PM
 */
package elitech.vietnam.copyweatherfashion.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import elitech.vietnam.copyweatherfashion.MainActivity;
import elitech.vietnam.copyweatherfashion.R;
import elitech.vietnam.copyweatherfashion.fragments.self.SelfFragment;
import elitech.vietnam.copyweatherfashion.fragments.settings.SettingsFragment;
import elitech.vietnam.copyweatherfashion.fragments.styler.StylerFragment;
import elitech.vietnam.copyweatherfashion.fragments.today.TodayFragment;
import elitech.vietnam.copyweatherfashion.fragments.weekly.WeeklyFragment;

/**
 * @author Cong
 *
 */
public class BaseFragment extends Fragment {
	
	public static final String FRAGMENTTAG = "FRAGMENT_TAG";
	private static final String TAG = "BASE_TAG";

	boolean isInited = false;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.base, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (isInited) {
			/*
			 * Pop child back stack fragments if exists
			 */
			popAllFragment();
			return;
		}
		isInited = true;
		Bundle args = getArguments();
		if (args == null || args.getString(TAG) == null || args.getString(TAG).length() == 0)
			throw new IllegalArgumentException("Base TAG not set");
		
		if (args.getString(TAG).equals(MainActivity.TAB_TODAY))
			replaceFragment(new TodayFragment(), false);
		else if (args.getString(TAG).equals(MainActivity.TAB_WEEKLY))
			replaceFragment(new WeeklyFragment(), false);
		else if (args.getString(TAG).equals(MainActivity.TAB_STYLER))
			replaceFragment(new StylerFragment(), false);
		else if (args.getString(TAG).equals(MainActivity.TAB_SELF))
			replaceFragment(new SelfFragment(), false);
		else if (args.getString(TAG).equals(MainActivity.TAB_SETTINGS))
			replaceFragment(new SettingsFragment(), false);
	}
	
	/**
	 * Replace current fragment with new fragment in this base
	 * @param fragment
	 * @param addToBackStack
	 */
	public void replaceFragment(Fragment fragment, boolean addToBackStack) {
		FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
		if (addToBackStack) {
			transaction.addToBackStack(getTag());
		}
		transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
		transaction.replace(R.id.container_framelayout, fragment, fragment.getClass().getName());
		transaction.commit();
	}
	
	/**
	 * Pop fragment back stack
	 * @return
	 */
	public boolean popFragment() {
		boolean isPop = false;
		if (getChildFragmentManager().getBackStackEntryCount() > 0) {
			isPop = true;
			getChildFragmentManager().popBackStack();
		}
		return isPop;
	}
	
	/**
	 * Pop all back stack and return to first fragment
	 * @return
	 */
	public boolean popAllFragment() {
		if (getChildFragmentManager().getBackStackEntryCount() > 0) {
			return getChildFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
		}
		return false;
	}
	
	public static Bundle newArs(String tag) {
		Bundle bundle = new Bundle();
		bundle.putString(TAG, tag);
		return bundle;
	}
}
